package com.chuangsi.project.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.chuangsi.project.admin.domain.ApproveConfig;
import com.chuangsi.project.admin.dto.admin.approve.*;
import com.chuangsi.project.admin.mapper.ApproveConfigMapper;
import com.chuangsi.project.admin.service.ApproveConfigService;
import com.chuangsi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 审批流程配置表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-03-23
 */
@Service
public class ApproveConfigServiceImpl extends ServiceImpl<ApproveConfigMapper, ApproveConfig> implements ApproveConfigService {

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public List<ResApproveConfigListDto> configList() {

        List<ResApproveConfigListDto> list = new ArrayList<>();
        ResApproveConfigListDto r1 = new ResApproveConfigListDto();
        r1.setType("合同审批");
        List<ApproveConfig> list1 = this.list(new LambdaQueryWrapper<ApproveConfig>().eq(ApproveConfig::getType, "1").last("limit 1"));
        if(!list1.isEmpty()){
            r1.setUpdateTime(list1.get(0).getUpdateTime());
        }
        ResApproveConfigListDto r2 = new ResApproveConfigListDto();
        r2.setType("订单审批");
        List<ApproveConfig> list2 = this.list(new LambdaQueryWrapper<ApproveConfig>().eq(ApproveConfig::getType, "2").last("limit 1"));
        if(!list2.isEmpty()){
            r2.setUpdateTime(list2.get(0).getUpdateTime());
        }
        ResApproveConfigListDto r3 = new ResApproveConfigListDto();
        r3.setType("退保审批");
        List<ApproveConfig> list3 = this.list(new LambdaQueryWrapper<ApproveConfig>().eq(ApproveConfig::getType, "3").last("limit 1"));
        if(!list3.isEmpty()){
            r3.setUpdateTime(list3.get(0).getUpdateTime());
        }

        list.add(r1);
        list.add(r2);
        list.add(r3);
        return list;
    }

    @Override
    public List<ResApproveConfigDetailDto> configDetail(ReqApproveConfigDetailDto req) {
        return this.list(new LambdaQueryWrapper<ApproveConfig>().eq(ApproveConfig::getType,req.getType()))
                .stream().map(m->{
            ResApproveConfigDetailDto res = new ResApproveConfigDetailDto();
            res.setUserId(m.getUserId());
            res.setUserName(sysUserService.selectNickNameById(m.getUserId()));
            res.setSort(m.getSort());
            return res;
        }).collect(Collectors.toList());
    }

    @Override
    public boolean updateConfig(ReqAppoveConfigUpdateDto req) {
        remove(new LambdaQueryWrapper<ApproveConfig>().eq(ApproveConfig::getType,req.getType()));
        List<ApproveConfig> addList = new ArrayList<>();
        for(ReqApproveConfigUpdateSubDto i : req.getUserIds()){
            ApproveConfig approveConfig = new ApproveConfig();
            approveConfig.setUserId(i.getUserId());
            approveConfig.setSort(i.getSort());
            approveConfig.setType(req.getType());
            addList.add(approveConfig);
        }
        return saveBatch(addList);
    }
}
